package org.geohunter.activity.osmmap;

import org.andnav.osm.util.GeoPoint;
import org.andnav.osm.views.overlay.OpenStreetMapViewOverlay;
import org.andnav.osm.views.overlay.OpenStreetMapViewSimpleLocationOverlay;
import org.geohunter.GeoFix;
import org.geohunter.GeoFixProvider;
import org.geohunter.GeocacheFilter;
import org.geohunter.Refresher;
import org.geohunter.activity.main.GuiState;
import org.geohunter.activity.main.TabBase;
import org.geohunter.activity.map.OverlayManager;
import org.geohunter.menuactions.MenuActions;

import android.graphics.drawable.Drawable;
import android.util.Log;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

/** Like the regular MapTab, but uses osmdroid as map view/controller */
public class OsmMapTab extends TabBase {
    private RelativeLayout mRelativeLayout;
    private OsmMapView mMapView;
    private OpenStreetMapViewSimpleLocationOverlay mMyLocationOverlay;
    private final GeoFixProvider mGeoFixProvider;
    private final GuiState mGuiState;
    private final OverlayManager<OpenStreetMapViewOverlay> mOverlayManager;

    public OsmMapTab(RelativeLayout contentView, Drawable icon,
            MenuActions menuActions, GuiState guiState, GeoFixProvider geoFixProvider,
            OsmMapView mapView, 
            OpenStreetMapViewSimpleLocationOverlay myLocationOverlay,
            OverlayManager<OpenStreetMapViewOverlay> overlayManager) {
        super("tab_osm", "OSM Map", contentView, icon, menuActions);
        mRelativeLayout = contentView;
        mGeoFixProvider = geoFixProvider;
        mGuiState = guiState;
        mMapView = mapView;
        mMyLocationOverlay = myLocationOverlay;
        mOverlayManager = overlayManager;
    }
    
    @Override
    public void onCreate() {
        Log.d("geohunter", "OsmMapTab.onCreate()");
        
        mOverlayManager.initArea();
        mRelativeLayout.addView(mMapView, new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        mOverlayManager.setCacheFilter(mGuiState.getActiveFilter());
        mMapView.setBuiltInZoomControls(true);
        mMapView.getController().setZoom(12);
        GeoPoint center = new GeoPoint(mGeoFixProvider.getLocation().getLatitude(),
                mGeoFixProvider.getLocation().getLongitude());
        mMapView.getController().setCenter(center);
    }

    @Override
    public void onResume() {
        mGeoFixProvider.addObserver(mMyLocationRefresher);
        
        mOverlayManager.refreshIfNeeded();
        mMapView.updateIfMoved();
    }

    @Override
    public void onPause() {
        mGeoFixProvider.removeObserver(mMyLocationRefresher);
    }
    
    @Override
    public void onDataViewChanged(GeocacheFilter filter, boolean isTabActive) {
        mOverlayManager.databaseChanged();
        mOverlayManager.setCacheFilter(filter);
        mOverlayManager.invalidate();
        if (isTabActive) {
            mOverlayManager.refreshIfNeeded();
        }
    }
    
    private class MyLocationRefresher implements Refresher {
        @Override
        public void forceRefresh() {
            GeoFix geoFix = mGeoFixProvider.getLocation();
            mMyLocationOverlay.setLocation(new GeoPoint(geoFix.getLatitude(), geoFix.getLongitude()));
        }

        @Override
        public void refresh() {
            forceRefresh();
        }
    }
    private MyLocationRefresher mMyLocationRefresher = new MyLocationRefresher();
    
}
